{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "### load\n",
    "\n",
    "load 方法用于加载 Scikit-learn 内置的小型数据集。这些数据集，无需下载，通常用于教学或快速测试模型。\n",
    "\n",
    "load_iris: 鸢尾花数据集（分类任务）。\n",
    "\n",
    "load_digits: 手写数字数据集（分类任务）。\n",
    "\n",
    "load_wine: 葡萄酒数据集（分类任务）。\n",
    "\n",
    "load_breast_cancer: 乳腺癌数据集（分类任务）。"
   ],
   "id": "f269e204d4216ddf"
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-01-11T12:57:57.655566Z",
     "start_time": "2025-01-11T12:57:57.585166Z"
    }
   },
   "source": [
    "from sklearn.datasets import load_iris\n",
    "\n",
    "# 加载鸢尾花数据集\n",
    "data = load_iris()\n",
    "\n",
    "# 数据集结构\n",
    "print(\"特征数据:\\n\", data.data[:5])  # 特征矩阵\n",
    "print(\"目标标签:\\n\", data.target[:5])  # 目标标签\n",
    "print(\"特征名称:\\n\", data.feature_names)  # 特征名称\n",
    "print(\"目标名称:\\n\", data.target_names)  # 目标类别名称"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "特征数据:\n",
      " [[5.1 3.5 1.4 0.2]\n",
      " [4.9 3.  1.4 0.2]\n",
      " [4.7 3.2 1.3 0.2]\n",
      " [4.6 3.1 1.5 0.2]\n",
      " [5.  3.6 1.4 0.2]]\n",
      "目标标签:\n",
      " [0 0 0 0 0]\n",
      "特征名称:\n",
      " ['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']\n",
      "目标名称:\n",
      " ['setosa' 'versicolor' 'virginica']\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "### fetch\n",
    "\n",
    "fetch 方法用于从远程服务器下载较大的数据集。这些数据集通常用于更复杂的任务或基准测试。\n",
    "\n",
    "fetch_openml: 从 OpenML 平台下载数据集（支持分类和回归任务）。\n",
    "\n",
    "fetch_california_housing: 加利福尼亚房价数据集（回归任务）。\n",
    "\n",
    "fetch_20newsgroups: 20 个新闻组文本数据集（文本分类任务）。\n",
    "\n",
    "fetch_lfw_people: 人脸识别数据集（分类任务）。\n",
    "\n",
    "fetch_olivetti_faces: Olivetti 人脸数据集（分类任务）。"
   ],
   "id": "e419350ae195f9a1"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-11T12:14:42.087059Z",
     "start_time": "2025-01-11T12:14:42.080765Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 查找 Scikit-learn 的数据集存储路径\n",
    "from sklearn import datasets\n",
    "data_home = datasets.get_data_home()\n",
    "print(data_home)"
   ],
   "id": "859f4d9353c1c0f5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C:\\Users\\DELL\\scikit_learn_data\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "将下载的 cal_housing.tgz 文件放入该路径后，即可从本地加载数据集。",
   "id": "3337f5a1a61b4430"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-11T12:26:46.733319Z",
     "start_time": "2025-01-11T12:26:46.676584Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn import datasets\n",
    "import tarfile\n",
    "import numpy as np\n",
    "import os\n",
    "\n",
    "data_home = datasets.get_data_home()\n",
    "archive_path = os.path.join(data_home, 'cal_housing.tgz')\n",
    "\n",
    "with tarfile.open(archive_path, mode=\"r:gz\") as f:\n",
    "    cal_housing = np.loadtxt(\n",
    "        f.extractfile(\"CaliforniaHousing/cal_housing.data\"), delimiter=\",\"\n",
    "    )\n",
    "\n",
    "# 调整列顺序\n",
    "columns_index = [8, 7, 2, 3, 4, 5, 6, 1, 0]\n",
    "cal_housing = cal_housing[:, columns_index]\n",
    "\n",
    "# 定义特征名称\n",
    "feature_names = [\n",
    "    \"MedInc\", \"HouseAge\", \"AveRooms\", \"AveBedrms\", \"Population\", \n",
    "    \"AveOccup\", \"Latitude\", \"Longitude\"\n",
    "]\n",
    "\n",
    "# 分离目标值和特征值\n",
    "target, data = cal_housing[:, 0], cal_housing[:, 1:]\n",
    "\n",
    "# 调整特征值\n",
    "data[:, 2] /= data[:, 5]  # avg rooms = total rooms / households\n",
    "data[:, 3] /= data[:, 5]  # avg bed rooms = total bed rooms / households\n",
    "data[:, 5] = data[:, 4] / data[:, 5]  # avg occupancy = population / households\n",
    "\n",
    "# 调整目标值\n",
    "target = target / 100000.0\n",
    "print(data)"
   ],
   "id": "21523afd7b548617",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[   8.3252       41.            6.98412698 ...    2.55555556\n",
      "    37.88       -122.23      ]\n",
      " [   8.3014       21.            6.23813708 ...    2.10984183\n",
      "    37.86       -122.22      ]\n",
      " [   7.2574       52.            8.28813559 ...    2.80225989\n",
      "    37.85       -122.24      ]\n",
      " ...\n",
      " [   1.7          17.            5.20554273 ...    2.3256351\n",
      "    39.43       -121.22      ]\n",
      " [   1.8672       18.            5.32951289 ...    2.12320917\n",
      "    39.43       -121.32      ]\n",
      " [   2.3886       16.            5.25471698 ...    2.61698113\n",
      "    39.37       -121.24      ]]\n"
     ]
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-11T12:27:31.432761Z",
     "start_time": "2025-01-11T12:27:31.374578Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#使用 fetch_openml 从 OpenML 平台加载数据集\n",
    "from sklearn.datasets import fetch_openml\n",
    "\n",
    "# 加载加州房价数据集\n",
    "data = fetch_openml(name=\"california_housing\", version=1, as_frame=True)\n",
    "print(data)"
   ],
   "id": "8dc9d62443be6866",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'data':        longitude  latitude  housing_median_age  total_rooms  total_bedrooms  \\\n",
      "0        -122.23     37.88                  41          880           129.0   \n",
      "1        -122.22     37.86                  21         7099          1106.0   \n",
      "2        -122.24     37.85                  52         1467           190.0   \n",
      "3        -122.25     37.85                  52         1274           235.0   \n",
      "4        -122.25     37.85                  52         1627           280.0   \n",
      "...          ...       ...                 ...          ...             ...   \n",
      "20635    -121.09     39.48                  25         1665           374.0   \n",
      "20636    -121.21     39.49                  18          697           150.0   \n",
      "20637    -121.22     39.43                  17         2254           485.0   \n",
      "20638    -121.32     39.43                  18         1860           409.0   \n",
      "20639    -121.24     39.37                  16         2785           616.0   \n",
      "\n",
      "       population  households  median_income ocean_proximity  \n",
      "0             322         126         8.3252        NEAR BAY  \n",
      "1            2401        1138         8.3014        NEAR BAY  \n",
      "2             496         177         7.2574        NEAR BAY  \n",
      "3             558         219         5.6431        NEAR BAY  \n",
      "4             565         259         3.8462        NEAR BAY  \n",
      "...           ...         ...            ...             ...  \n",
      "20635         845         330         1.5603          INLAND  \n",
      "20636         356         114         2.5568          INLAND  \n",
      "20637        1007         433         1.7000          INLAND  \n",
      "20638         741         349         1.8672          INLAND  \n",
      "20639        1387         530         2.3886          INLAND  \n",
      "\n",
      "[20640 rows x 9 columns], 'target': 0        452600\n",
      "1        358500\n",
      "2        352100\n",
      "3        341300\n",
      "4        342200\n",
      "          ...  \n",
      "20635     78100\n",
      "20636     77100\n",
      "20637     92300\n",
      "20638     84700\n",
      "20639     89400\n",
      "Name: median_house_value, Length: 20640, dtype: int64, 'frame':        longitude  latitude  housing_median_age  total_rooms  total_bedrooms  \\\n",
      "0        -122.23     37.88                  41          880           129.0   \n",
      "1        -122.22     37.86                  21         7099          1106.0   \n",
      "2        -122.24     37.85                  52         1467           190.0   \n",
      "3        -122.25     37.85                  52         1274           235.0   \n",
      "4        -122.25     37.85                  52         1627           280.0   \n",
      "...          ...       ...                 ...          ...             ...   \n",
      "20635    -121.09     39.48                  25         1665           374.0   \n",
      "20636    -121.21     39.49                  18          697           150.0   \n",
      "20637    -121.22     39.43                  17         2254           485.0   \n",
      "20638    -121.32     39.43                  18         1860           409.0   \n",
      "20639    -121.24     39.37                  16         2785           616.0   \n",
      "\n",
      "       population  households  median_income  median_house_value  \\\n",
      "0             322         126         8.3252              452600   \n",
      "1            2401        1138         8.3014              358500   \n",
      "2             496         177         7.2574              352100   \n",
      "3             558         219         5.6431              341300   \n",
      "4             565         259         3.8462              342200   \n",
      "...           ...         ...            ...                 ...   \n",
      "20635         845         330         1.5603               78100   \n",
      "20636         356         114         2.5568               77100   \n",
      "20637        1007         433         1.7000               92300   \n",
      "20638         741         349         1.8672               84700   \n",
      "20639        1387         530         2.3886               89400   \n",
      "\n",
      "      ocean_proximity  \n",
      "0            NEAR BAY  \n",
      "1            NEAR BAY  \n",
      "2            NEAR BAY  \n",
      "3            NEAR BAY  \n",
      "4            NEAR BAY  \n",
      "...               ...  \n",
      "20635          INLAND  \n",
      "20636          INLAND  \n",
      "20637          INLAND  \n",
      "20638          INLAND  \n",
      "20639          INLAND  \n",
      "\n",
      "[20640 rows x 10 columns], 'categories': None, 'feature_names': ['longitude', 'latitude', 'housing_median_age', 'total_rooms', 'total_bedrooms', 'population', 'households', 'median_income', 'ocean_proximity'], 'target_names': ['median_house_value'], 'DESCR': 'Median house prices for California districts derived from the 1990 census.\\n\\nDownloaded from openml.org.', 'details': {'id': '43939', 'name': 'california_housing', 'version': '1', 'description_version': '1', 'format': 'ARFF', 'upload_date': '2022-06-06T11:27:34', 'licence': 'public', 'url': 'https://api.openml.org/data/v1/download/22102987/california_housing.arff', 'parquet_url': 'https://openml1.win.tue.nl/datasets/0004/43939/dataset_43939.pq', 'file_id': '22102987', 'default_target_attribute': 'median_house_value', 'visibility': 'public', 'original_data_url': 'https://www.kaggle.com/datasets/camnugent/california-housing-prices', 'minio_url': 'https://openml1.win.tue.nl/datasets/0004/43939/dataset_43939.pq', 'status': 'active', 'processing_date': '2022-06-06 11:28:03', 'md5_checksum': '38cc9f7060e510f2f9f8072e250c5c35'}, 'url': 'https://www.openml.org/d/43939'}\n"
     ]
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### train_test_split样本切分",
   "id": "1e056c4417d9e538"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-11T12:58:00.829944Z",
     "start_time": "2025-01-11T12:58:00.806840Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "import numpy as np\n",
    "\n",
    "# 加载鸢尾花数据集\n",
    "iris = load_iris()\n",
    "\n",
    "# 特征矩阵 (X) 和目标标签 (y)\n",
    "X = iris.data  # 特征数据\n",
    "y = iris.target  # 目标标签\n",
    "\n",
    "# 使用 train_test_split 切分数据集\n",
    "# 参数说明：\n",
    "# - test_size: 测试集比例，这里设置为 20%\n",
    "# - random_state: 随机种子，确保每次运行结果一致\n",
    "# - stratify: 按目标标签分层抽样，确保训练集和测试集的标签分布一致\n",
    "X_train, X_test, y_train, y_test = train_test_split(\n",
    "    X, y, test_size=0.2, random_state=42, stratify=y\n",
    ")\n",
    "\n",
    "# 输出切分结果\n",
    "print(\"训练集特征形状:\", X_train.shape)\n",
    "print(\"测试集特征形状:\", X_test.shape)\n",
    "print(\"训练集标签分布:\\n\", y_train)\n",
    "print(\"测试集标签分布:\\n\", y_test)"
   ],
   "id": "dfcf80962ad30793",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练集特征形状: (120, 4)\n",
      "测试集特征形状: (30, 4)\n",
      "训练集标签分布:\n",
      " [0 2 1 0 1 2 1 2 2 2 2 1 1 1 1 0 0 2 2 0 1 0 2 0 1 2 2 0 2 0 0 1 1 0 2 2 1\n",
      " 1 2 1 0 1 0 2 0 0 2 0 0 0 0 1 2 1 0 2 1 2 0 2 0 1 2 0 1 1 2 1 1 2 0 0 0 2\n",
      " 1 2 1 2 2 1 0 2 1 0 2 0 2 1 1 0 1 2 0 0 2 2 2 1 2 0 2 1 2 2 0 1 1 1 1 1 0\n",
      " 2 1 1 0 0 0 0 1 0]\n",
      "测试集标签分布:\n",
      " [0 2 1 1 0 1 0 0 2 1 2 2 2 1 0 0 0 1 1 2 0 2 1 2 2 1 1 0 2 0]\n"
     ]
    }
   ],
   "execution_count": 3
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
